import { defineComponent, removeComponent, hasComponent } from './ecs';
import { Collision, Glyph, Impassable, Opaque } from './components';
import { describe } from '$lib';
import { log } from './log';
import { encode } from './util';
import { Clock } from './clock';

export const Interact = defineComponent('Interact', {});

export const Door = defineComponent('Door', {});

export const OpenAction = (target, delay) => (world, e) => {
  Clock.time[e] += delay;
  if (hasComponent(world, Impassable, target)) {
    removeComponent(world, Impassable, target);
    removeComponent(world, Collision, target);
    removeComponent(world, Opaque, target);
    Glyph.glyph[target] = encode('/');
    log(`${describe(world, e)} opens the door!`);
  } else {
    log(`${describe(world, e)} tries to open an open door!`);
  }
};
